package com.wiegand.util {
	import com.wiegand.loader.SwfLoader;
	import com.caurina.transitions.Tweener;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author wie-gand
	 */
	public class MovingContainerBox extends Sprite {
		
		private var _mask : Sprite;
		
		private var _width : int = 200;
		private var _height : int = 150;
		private var _container : Sprite;
		private var _showPosition : Boolean;
		
		public static const EVENT_MOVEACTION : String = "moveaction";
		private var _scrollBar : Sprite;
		private var _scrollBt : Sprite;
		private var _bg : Sprite;
		private var _scroller : Sprite;
		private var _slot : Sprite;

		public function MovingContainerBox( content : Sprite, width : int, height : int, blendColor : uint = 0x000000, showPosition:Boolean=false ) {
			_width = width;
			_height = height;
			_showPosition = showPosition;
			init(content,blendColor);
		}
		
		private function init(content:Sprite,blendColor : uint):void{			
			_bg = ColorRectangle.create(0x000000, .75, _width, _height );
//			_bg.buttonMode = true;
//			_bg.mouseEnabled = true;
			_bg.alpha = 0;
			addChild( _bg );
			
			_container = new Sprite();
			_container.mouseEnabled = false;
			content.mouseEnabled = false;	
			_container.addChild( content );
			addChild( _container );
			
			_mask = ColorRectangle.create(0xff0000, .3, _width, _height );
			_mask.buttonMode = true;
			_mask.mouseEnabled = true;
			addChild( _mask );
			
			_container.mask = _mask;
			
			_slot = SwfLoader.getInstance().getDisplayObject("Slot") as Sprite;
			_slot.y = _height;
			_slot.width = _width*1.3;
			_slot.x = -(_slot.width-_width)/2;
			_slot.scaleY = 0.01;
			addChild( _slot );
			
			_scroller = new Sprite();
			_scroller.visible = _showPosition;
			_scroller.x = _width+1;
			addChild(_scroller);
			
			_scrollBar = ColorRectangle.create(blendColor,.5,3,_height);
			_scroller.addChild( _scrollBar );
			
			var btPerc : int = Math.round( (_height*100)/_container.height );
			var btHeight : int = Math.round( (_height*btPerc)/100 );
			
			_scrollBt = ColorRectangle.create(blendColor,1,3,btHeight );
			_scroller.addChild( _scrollBt );
		}
		
		public function open( delay : Number = 0 ):void{
			removeTweens();
			removeListeners();
		
			Tweener.addTween( _slot, { scaleY:3, delay:delay, time:.5, transition:"easeoutback" } );
			Tweener.addTween( _slot, { scaleY:1.5, delay:delay+.5, time:.2, transition:"easeoutexpo" } );
			
			_bg.alpha = 0;
			_bg.height = 1;
			_bg.y = _height;
			Tweener.addTween( _bg, { alpha:1, y:0, height:_height, delay:delay+.7, time:2, transition:"easeoutquart" } );
			
			_container.alpha = 0;
			_container.y = _height;
			Tweener.addTween( _container, { alpha:1, y:0, delay:delay+2.5, time:2, transition:"easeoutback" } );

			_scroller.alpha = 0;
			_scroller.scaleX = .1;
			Tweener.addTween( _scroller, { alpha:1, scaleX:1, delay:delay+4, time:1, transition:"easeoutquart", onComplete:startHittest } );
		}

		public function close( delay : Number = 0 ):void{
			removeTweens();
			removeListeners();
			
			Tweener.addTween( _scroller, { alpha:0, scaleX:.1, delay:delay, time:.1, transition:"easeoutquart" } );
			Tweener.addTween( _container, { alpha:0, y:_height, delay:delay+.1, time:.5, transition:"easeoutback" } );
			Tweener.addTween( _bg, { alpha:0, y:_height, height:1, delay:delay+.6, time:.6, transition:"easeoutquart" } );
			Tweener.addTween( _slot, { scaleY:.1, delay:delay+1.2, time:.5, transition:"easeoutexpo" } );
		}
		
		private function startHittest(e:MouseEvent=null):void{
			_container.alpha=1;
			
			_mask.removeEventListener( MouseEvent.MOUSE_OVER, startHittest );	
			_mask.addEventListener( MouseEvent.MOUSE_OUT, stopHittest );	
			_mask.buttonMode = false;
			_mask.mouseEnabled = false;
			addEventListener(Event.ENTER_FRAME, hittest);			
		}
		
		private function stopHittest(e:MouseEvent=null):void{
			_mask.removeEventListener( MouseEvent.MOUSE_OUT, stopHittest );	
			_mask.addEventListener( MouseEvent.MOUSE_OVER, startHittest );
			_mask.buttonMode = true;
			_mask.mouseEnabled = true;	
			addEventListener(Event.ENTER_FRAME, hittest);
		}
		
		private function hittest(e:Event):void{
			var clip : DisplayObject = _mask.parent.parent.parent;
			if(_mask.hitTestPoint(clip.mouseX, clip.mouseY) ) move();
		}
		
		private function move() : void {
			dispatchEvent( new Event(EVENT_MOVEACTION) );			
						
//			var xperc : int = ( _mask.mouseX * 100 ) / _mask.width;
			var yperc : int = ( _mask.mouseY * 100 ) / _mask.height;
			
//			var xpercImage : int = ( _container.mouseX * 100 ) / _container.width;
			var ypercImage : int = ( _container.mouseY * 100 ) / _container.height;
						
//			var xpercdiff : int = xpercImage - xperc;
			var ypercdiff : int = ypercImage - yperc;
			
//			var oneImagePercX : int =  _container.width / 100;
			var oneImagePercY : int =  _container.height / 100;
						
//			var xdiff : int = xpercdiff * oneImagePercX;
			var ydiff : int = ypercdiff * oneImagePercY;

			Tweener.removeTweens( _container );
//			Tweener.addTween( _container, { x:_container.x + xdiff, y:_container.y + ydiff, time:3, transition:"easeoutexpo", onUpdate:posScroller } );
			Tweener.addTween( _container, { y:_container.y + ydiff, time:5, transition:"easeoutquart", rounded:true, onUpdate:posScroller } );
		
		}			
		
		private function posScroller():void{
//			_container.y = Math.round(_container.y);
			
			var btPerc : int = Math.round( (Math.abs(_container.y)*100)/(_container.height-_height));
		
			var btPos : int = Math.round( (btPerc*_height)/100 )-_scrollBt.height/2;
			btPos = Math.max( 0, btPos );
			btPos = Math.min( btPos, _height-_scrollBt.height );
			_scrollBt.y = btPos;		
		}			
		
		public function getBoxWidth() : int{
			return _width;
		}		
		
		public function getContainerX() : int{
			return _container.x;
		}
				
		public function getContainerY() : int{
			return _container.y;
		}		
		
		public function resize(h:int) : void {
			_height=h;
			
			_mask.height=h;
			_scrollBar.height=h;
			
			var btPerc : int = Math.round( (_height*100)/_container.height );
			var btHeight : int = Math.round( (_height*btPerc)/100 );
			_scrollBt.height=btHeight;
			
			_container.y = 0;
		}

		private function removeListeners():void{
			_mask.removeEventListener( MouseEvent.MOUSE_OUT, stopHittest );	
			_mask.addEventListener( MouseEvent.MOUSE_OVER, startHittest );	
			removeEventListener(Event.ENTER_FRAME, hittest);			
		}
	
		private function removeTweens():void{
			Tweener.removeTweens( _slot );
			Tweener.removeTweens( _bg );
			Tweener.removeTweens( _container );
			Tweener.removeTweens( _scroller );
		}

		public function destroy():void{
			removeListeners();
			removeTweens();
			
			removeChild( _slot );
			removeChild( _bg );
			removeChild( _container );
			removeChild( _mask );
			removeChild(_scroller);
		}
  
	}
}
